// 生成随机字符串作为列表的key值
import {
	generateRandomString
} from "@/utils/util.js"

const IslandType = {
	MENTALHEALTH: {
		value: "mentalHealth",
		url: "",
		name: "心理健康",
		count: 0,
		haved: 0,
		intro: "",
		recaps: {
			'小学': "培养良好的情绪管理和社交能力",
			'初中': '重视心理健康，应对青春期挑战',
			'高中': "强化心理健康，应对压力和高考",
			'大学': "关注心理健康，适应大学生活",
			'在职': "维护心理健康，应对工作压力和挑战"
		},
	},
	DEVELOPMENT: {
		value: "development",
		url: "",
		name: "能力发展",
		count: 0,
		haved: 0,
		intro: "",
		recaps: {
			'小学': "了解能力发展特点，激发无限潜能",
			'初中': '发掘发展方向，为未来规划提供指导',
			'高中': "明确发展重点，为志愿填报提供依据",
			'大学': "提升能力，培养职业素养和领导力",
			'在职': "提升能力，实现职业晋升和个人成长"
		},
	},
	FAMILY: {
		value: "family",
		url: "",
		name: "家庭情况",
		count: 0,
		haved: 0,
		intro: "",
		recaps: {
			'小学': "了解家庭影响，提供教育建议",
			'初中': '关注家庭环境，指导教育成长',
			'高中': "关注家庭变化，增强亲子沟通和理解"
		},
	},
	INTIMATERELATIONSHIP: {
		value: "intimateRelationship",
		url: "",
		name: "亲密关系",
		count: 0,
		haved: 0,
		intro: "",
		recaps: {
			'大学': "建立健康的关系，提升交往技能",
			'在职': "改善亲密关系，实现工作与生活平衡"
		},
	},
	COLLEGELIFE: {
		value: "collegeLife",
		url: "",
		name: "校园生活",
		count: 0,
		haved: 0,
		intro: "",
		recaps: {
			'小学': "优化校园生活，提高学习参与度",
			'初中': '焕发校园活力，点燃学习热情',
			'高中': "提升学习效率，增强团队合作能力",
			'大学': "丰富校园生活，拓展社交实践经验"
		},
	},
	CAREER: {
		value: "career",
		url: "",
		name: "职业发展",
		count: 0,
		haved: 0,
		intro: "",
		recaps: {
			'大学': "规划职业发展路径，为就业做准备",
			'在职': "优化职业策略，开拓广阔前景"
		},
	},
	ADOLESCENT: {
		value: "adolescent",
		url: "",
		name: "青春期",
		count: 0,
		haved: 0,
		intro: "",
		recaps: {
			'初中': "在青春期树立正确的价值观和人生观"
		},
	},
	NETADDICTION: {
		value: "netAddiction",
		url: "",
		name: "网络成瘾",
		count: 0,
		haved: 0,
		intro: "",
		recap: "预防网络成瘾，培养自律管理能力",
		recaps: {
			'高中': "预防网络成瘾，培养自律管理能力",
			'初中': "警惕网络成瘾，培养健康上网习惯"
		},
	},
	FILLVOLUNTEERS: {
		value: "fillVolunteers",
		url: "",
		name: "志愿填报",
		count: 0,
		haved: 0,
		intro: "",
		recaps: {
			'高中': "精准填报志愿，开启理想的大学生涯"
		},
	},
}
// 视图层级可视化
// 不可更改 !!!
Object.freeze(IslandType)

// 阶段类型
export const StageType = {
	SECONDARYSCHOOL: "小学阶段",
	MIDDLESCHOOL: "初中阶段",
	HIGHSCHOOL: "高中阶段",
	UNIVERSITY: "大学阶段",
	WORK: "在职阶段"
}

export const StageIslandMap = [{
	stage: StageType.SECONDARYSCHOOL,
	icon: '/static/image/stage-secondary-school.png',
}, {
	stage: StageType.MIDDLESCHOOL,
	icon: '/static/image/stage-middle-school.png',
}, {
	stage: StageType.HIGHSCHOOL,
	icon: '/static/image/stage-high-school.png',
}, {
	stage: StageType.UNIVERSITY,
	icon: '/static/image/stage-university.png',
}, {
	stage: StageType.WORK,
	icon: '/static/image/stage-work.png',
}]

/**
 * 获取阶段对应的维度/岛屿组 
 * 心灵岛屿
 */
export function getIslandMapByStage(stage, islandTypes, urlParams = {
	startStr: "https://staticv3.youzy.cn/images/ad/miniapp/ucp/image/island-"
}, duration = 880) {
	// 获取阶段数据模板
	const index = StageIslandMap.findIndex(item => item.stage == stage)
	if (index < 0) return index

	// 复制数据模板
	const currentStage = {
		...StageIslandMap[index]
	}

	// 动画参数 时间间隔
	let timeCount = 0
	// 复制阶段 岛屿模板
	const allIslandTypes = Object.values(IslandType)
	currentStage.islandTypes = islandTypes.filter(item => allIslandTypes.some(i => i.name == item.name)).map((item,
		i) => {
		// 动画参数配置
		const ISLAND = {
			...(allIslandTypes.find(i => i.name == item.name) || {}),
			key: generateRandomString(),
			animation: uni.createAnimation({
				duration,
				timingFunction: 'ease'
			}),
			animationData: {},
			timeCount,
			...item,
		}

		setIslandImage(ISLAND, urlParams)

		timeCount += 280

		return ISLAND
	})

	currentStage.space = getMarginMap(currentStage.islandTypes.length) || ""

	return currentStage
}
/**
 * 获取维度/岛屿 
 * islandParams { name: string, intro: string}
 */
export function getIslandByName(islandParams) {
	const propertyNames = Object.getOwnPropertyNames(islandParams)
	if (!propertyNames.some(name => name == "name")) {
		return `请提供维度名称：[${Object.getOwnPropertyNames(IslandType).join(",")}]`
	}
	// if (!propertyNames.some(name => name == "url")) {
	// 	return `请提供维度图片路径`
	// }

	// 多模块引用 原始模板不建议更改
	// 复制属性
	const island = {
		...IslandType[islandParams.name]
	}

	// 设置不同模块 维度展示图片
	setIslandImage(island, islandParams.urlParams)
	if (islandParams.intro) {
		setIslandIntro(island, islandParams.intro)
	}


	return island
}

// 岛屿 维度 图片路径 关键 /path/url.png 
// '../static/island-'+item.url+'.png'
// setIslandImage(island)
// setIslandImage(island,{startStr: "../static/"})
// setIslandImage(island,{startStr: "../static/",endStr: ".png"})
// ...
function setIslandImage(island, urlParams) {
	// value 是固定值
	island.url = island.value.replace(/[A-Z]/g, (match, ...args) => {
		return `-${match.toLocaleLowerCase()}`
	})

	if (urlParams) {
		if (urlParams?.startStr) island.url = `${urlParams.startStr}${island.url}`
		if (urlParams?.endStr) island.url = `${island.url}${urlParams.endStr}`

		const matchResult = island.url.match(/\.[png|gif|svg|jpg|webp]$/)
		if (!matchResult) {
			island.url = `${island.url}.${urlParams?.type||'png'}`
		}
	}
}

// 设置导语
// 待补充
function setIslandIntro(island, intro) {
	island.intro = intro
}


// 不同测评集组合 对应的布局
function getMarginMap(count) {
	return ({
		4: [
			`margin-top: 152rpx;margin-right:82rpx`,
			`margin-top: 24rpx;margin-left:60rpx`,
			`margin-top:-166rpx;margin-right:22rpx`,
			`margin-top: 18rpx;margin-left: 166rpx`
		],
		5: [
			`margin-top: 52rpx;margin-right:24rpx`,
			`margin-top: -30rpx;margin-left:136rpx`,
			`margin-top:-116rpx;margin-right:24rpx`,
			`margin-top:-110rpx;margin-left:20rpx`,
			`margin-top:-100rpx;margin-right:130rpx`,
		],
		6: [
			`margin-top: 52rpx;margin-right:24rpx`,
			`margin-top: -30rpx;margin-left:136rpx`,
			`margin-top:-136rpx;margin-right:22rpx`,
			`margin-top:-110rpx;margin-left:20rpx`,
			`margin-top:-172rpx;`,
			`margin-top:-82rpx;margin-left:114rpx`
		]
	} [count])
}